package Demo1.operation;

import Demo1.Book.Book;
import Demo1.Book.Booklist;

import java.util.Scanner;

public class DelOperation implements IOperation{
    @Override
    public void work(Booklist booklist) {
        Scanner scanner = new Scanner(System.in);
        int currentSize = booklist.getUsedSize();
        if (currentSize == 0){
            System.out.println("没有图书了，无法进行删除");
            return;
        }
        System.out.println("删除图书");
        System.out.println("请输入你要删除的书名：");
        String name = scanner.nextLine();
        int index = -1;
        //1、找到要删除的书
        for (int i = 0; i < currentSize; i++) {
            Book book = booklist.getBooks(i);
            if(book.getName().equals(name)){
                index = i;
                break;
            }
        }
        if (index == -1){
            System.out.println("没有你要删除的书");
            return;
        }
        for (int i = index; i < currentSize; i++) {
            Book book = booklist.getBooks(i+1);
            booklist.setBooks(i+1,book);
        }
        booklist.setUsedSize(currentSize-1);
    }
}
